const express = require('express')
const router = express.Router()
const {logI} = require('../log.js')
const {retError, retSuccess} = require('../utils/ret')
const userDb = require('../db/user-db')
const checkType = require('../utils/checkType')
const moment = require('moment')
const crypto = require('crypto')
const { v4: uuidv4} = require('uuid');

router
    .post('/', async(req, res) => {
        let {body: {maddr,code,pwd}} = req
        logI("A-S", maddr, "verify_auth", `${JSON.stringify(req.body)}`, "null")
        try {
            const error = checkType.verfiy(req.body)
            if(error){
                retError("verify_auth", maddr, 10001,  error.details[0].message, res);
            }else{
                let ret = {};
                ret = await userDb.verifyAuth(maddr, code)
                if(ret){
                    const startTimestamp = new Date(ret.updateTime).getTime();
                    const endTimestamp = new Date().getTime();
                    if(endTimestamp-startTimestamp<10*60*1000){
                        if(ret.code === code){
                            if(pwd || pwd === ""){
                                if(pwd === ""){
                                    retError("verify_auth", maddr, 10021, "Invalid Password", res);
                                }else{
                                    const updateTime = moment().format('YYYY-MM-DD HH:mm:ss')
                                    const newCode = crypto.randomInt(0, 1000000).toString().padStart(6, '0');
                                    const hashPass = crypto.createHmac('sha256', process.env.APP_SECRET)
                                        .update(pwd)
                                        .digest('hex')
                                    const token =  uuidv4();
                                    await userDb.updatePwd(hashPass,updateTime,token,newCode,maddr);
                                    retSuccess("verify_auth", maddr, 0, "", res);
                                }
                            }else{
                                const updateTime = moment().format('YYYY-MM-DD HH:mm:ss')
                                const newCode = crypto.randomInt(0, 1000000).toString().padStart(6, '0');
                                await userDb.updateVerify(1,updateTime,newCode,maddr);
                                retSuccess("verify_auth", maddr, 0, "", res);
                            }
                        }else{
                            retError("verify_auth", maddr, 10009, "verification code invalid", res);
                        }
                    }else{
                        retError("verify_auth", maddr, 10008, "verification code has expired", res);
                    }
                }else{
                    retError("verify_auth", maddr, 10007, "user does not exist", res);
                }
            }
        } catch (e) {
            retError("verify_auth", maddr, 10001, e, res);
        }
    })

module.exports = router
